package com.xkdx.guangguang.adapter;

import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils.TruncateAt;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.xkdx.caipiao.R;
import com.xkdx.guangguang.ManagerActivity;
import com.xkdx.guangguang.fragment.user.UserLoginFragment;
import com.xkdx.guangguang.module.statics.IConstants;
import com.xkdx.guangguang.shareclass.DaZhongDetailBean;
import com.xkdx.guangguang.shareclass.ShopCommentary;
import com.xkdx.guangguang.util.SharePrefenceUtil;
import com.xkdx.guangguang.wiget.TextViewEllipsize;

// 商场评论适配
public class ShopInfoCommentAdapter extends BaseAdapter {
	String url;
	Context context;
	List<ShopCommentary> commlist;
	List<DaZhongDetailBean> dazhongList;
	private int allnum;
	ImageLoader imageLoader;
	DisplayImageOptions options = new DisplayImageOptions.Builder().showStubImage(R.drawable.shop_empty)
			.showImageForEmptyUri(R.drawable.shop_empty).showImageOnFail(R.drawable.shop_empty)
			.resetViewBeforeLoading().cacheOnDisc().imageScaleType(ImageScaleType.EXACTLY)
			.bitmapConfig(Bitmap.Config.RGB_565).build();

	public ShopInfoCommentAdapter(Context context, List<ShopCommentary> list, List<DaZhongDetailBean> dazhongList,
			ImageLoader imageloder) {
		super();
		this.imageLoader = imageloder;
		this.commlist = list;
		this.context = context;
		this.dazhongList = dazhongList;
	}

	@Override
	public boolean isEnabled(int position) {
		return super.isEnabled(position);
	}

	@Override
	public int getCount() {

		int ggnum = 0;
		allnum = 0;
		ggnum = commlist.size();
		if ((dazhongList != null && dazhongList.size() != 0)) {
			if (dazhongList.size() > 0) {
				allnum = 1 + ggnum + 1 + dazhongList.size() + 1;
			}
		} else {
			allnum = 1 + ggnum + 1;
		}
		return allnum;
	}

	@Override
	public Object getItem(int position) {
		if (position < commlist.size()) {
			return commlist.get(position);
		} else if (position == commlist.size()) {
			return null;
		} else if (position == commlist.size() + 1) {
			return null;
		} else if (position < commlist.size() + 1 + 1 + dazhongList.size()) {
			int newposition = position - commlist.size() - 2;
			return dazhongList.get(newposition);
		} else {
			return null;
		}
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (position == 0) {
			View view = null;
			view = LayoutInflater.from(context).inflate(R.layout.shopcommentary_header, null);
			TextView tv_shop_commentarynum = (TextView) view.findViewById(R.id.tv_shop_commentarynum);
			ImageView tv_shop_commentarywant = (ImageView) view.findViewById(R.id.tv_shop_commentarywant);
			if (commlist.size() == 0) {
				tv_shop_commentarynum.setText("0");
			} else {
				tv_shop_commentarynum.setText(commlist.size() + "");
			}

			tv_shop_commentarywant.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					
					SharePrefenceUtil su_user = new SharePrefenceUtil(context, IConstants.SP_USER);
					String userID = su_user.getUserID();
					String LoginToken = su_user.getUserLoginToken();
					FragmentTransaction frCommentaryInfos = (FragmentTransaction) ((ManagerActivity) context)
							.getSupportFragmentManager().beginTransaction();
					if ("".equals(LoginToken)) { // 跳转到登陆页面 zhangshun
						AlertDialog.Builder ab = new AlertDialog.Builder(context);
						ab.setTitle("提示");
						ab.setMessage("请先登录");
						ab.setPositiveButton("确定", new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog, int which) {
								UserLoginFragment userLoginFragment = new UserLoginFragment();
								((ManagerActivity) context).getSupportFragmentManager().beginTransaction()
										.replace(R.id.fragment, userLoginFragment).addToBackStack(null).commit();
							}
						});
						ab.setNegativeButton("取消", null);
						ab.create().show();

					} else {
					}

					frCommentaryInfos.addToBackStack(null);
					frCommentaryInfos.commit();
				}
			});

			return view;
		} else if (position > 0 && position <= commlist.size()) {
			View view = null;
			view = LayoutInflater.from(context).inflate(R.layout.shop_commentaryitem, null);
			ImageView userhead = (ImageView) view.findViewById(R.id.iv_shopcommitem_userhead);
			TextView username = (TextView) view.findViewById(R.id.tv_shopcommitem_username);
			TextView commentaryDate = (TextView) view.findViewById(R.id.tv_shopcommitem_date);
			final TextViewEllipsize commentaryinfos = (TextViewEllipsize) view.findViewById(R.id.tv_shopcomitem_infos);
			final ImageView morecommentary = (ImageView) view.findViewById(R.id.btn_spcommitem_loadmore);

			int position0 = position - 1;
			username.setText(commlist.get(position0).getNickName() + ":");

			if (commlist.get(position0).getCreateTime() != null
					&& !(commlist.get(position0).getCreateTime().equals(""))) {
				commentaryDate.setText(DateParse(commlist.get(position0).getCreateTime()));
			} else {
				commentaryDate.setText("");
			}

			commentaryinfos.setText(commlist.get(position0).getCommentContent());
			commentaryinfos.setEllipsize(TruncateAt.END);
			commentaryinfos.setMaxLines(3);
			String picUrl = commlist.get(position0).getUserAvatars();
			// Log.i("picUrl",picUrl);
			imageLoader.displayImage(picUrl, userhead, options);

			morecommentary.setOnClickListener(new OnClickListener() {
				boolean flag = true;

				@Override
				public void onClick(View v) {

					if (flag) {
						int num = commentaryinfos.getLineCount();
						if (num >= 3) {
							commentaryinfos.setEllipsize(null);
							commentaryinfos.setMaxLines(100);
							morecommentary.setImageResource(R.drawable.commentaryclose);
							flag = false;
						}
					} else {
						int num = commentaryinfos.getLineCount();
						if (num >= 3) {
							morecommentary.setImageResource(R.drawable.commentaryopen);
						}
						commentaryinfos.setMaxLines(3);
						commentaryinfos.setEllipsize(TruncateAt.END);

						flag = true;
					}
				}
			});

			return view;
		} else if (position == (commlist.size() + 1)) {
			
			LinearLayout ll_loadmore = new LinearLayout(context);
			ll_loadmore.setOrientation(LinearLayout.HORIZONTAL);

			TextView tv_commentaryloadmore = new TextView(context);
			tv_commentaryloadmore.setText("查看更多逛逛用户评论  >");
			tv_commentaryloadmore.setBackgroundColor(context.getResources().getColor(R.color.gray_white));
			tv_commentaryloadmore.setTextSize(15.0f);
			tv_commentaryloadmore.setPadding(0, 15, 0, 15);
			tv_commentaryloadmore.setGravity(Gravity.CENTER);

			ll_loadmore.addView(tv_commentaryloadmore, new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
			ll_loadmore.setGravity(Gravity.CENTER);
			ll_loadmore.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// 跳转到评论详情列表
					FragmentTransaction ft = (FragmentTransaction) ((ManagerActivity) context)
							.getSupportFragmentManager().beginTransaction();

				}
			});
			return ll_loadmore;
		} else if (position == (commlist.size() + 2)) {
			LinearLayout dazhong_title = new LinearLayout(context);
			dazhong_title.setOrientation(LinearLayout.HORIZONTAL);
			dazhong_title.setBackgroundResource(R.drawable.dazhongdianping);
			return dazhong_title;
		} else {
			View view = null;
			view = LayoutInflater.from(context).inflate(R.layout.shop_dazhongitem, null);
			ImageView iv_dazhongxingji = (ImageView) view.findViewById(R.id.iv_dazhongxingji);
			TextView tv_dazhongcommitem_username = (TextView) view.findViewById(R.id.tv_dazhongcommitem_username);
			TextView tv_dazhongcommitem_date = (TextView) view.findViewById(R.id.tv_dazhongcommitem_date);
			TextView tv_dazhongcommitem_content = (TextView) view.findViewById(R.id.tv_dazhongcommitem_content);
			TextView tv_xingjinumber = (TextView) view.findViewById(R.id.tv_xingjinumber);
			LinearLayout ll_dazhongclick = (LinearLayout) view.findViewById(R.id.ll_dazhongclick);
			final int position0 = position - commlist.size() - 3;
			String name = dazhongList.get(position0).getUser_nickname();
			tv_dazhongcommitem_username.setText(dazhongList.get(position0).getUser_nickname() + ":");

			tv_dazhongcommitem_date.setText(DateParse(dazhongList.get(position0).getCreated_time()) + " 来自大众点评");
			tv_dazhongcommitem_content.setText(dazhongList.get(position0).getText_excerpt());
			tv_xingjinumber.setText(dazhongList.get(position0).getReview_rating());
			String picUrl = dazhongList.get(position0).getRating_s_img_url();
			// Log.i("picUrl", picUrl);
			if (picUrl.contains("dpfile.com/s/i/app/api/16_1star.png")) {
				iv_dazhongxingji.setImageResource(R.drawable.dazhong16_1star);
			} else if (picUrl.contains("dpfile.com/s/i/app/api/16_0star.png")) {
				iv_dazhongxingji.setImageResource(R.drawable.dazhong16_0star);
			} else if (picUrl.contains("dpfile.com/s/i/app/api/16_2star.png")) {
				iv_dazhongxingji.setImageResource(R.drawable.dazhong16_2star);
			} else if (picUrl.contains("dpfile.com/s/i/app/api/16_5star.png")) {
				iv_dazhongxingji.setImageResource(R.drawable.dazhong16_5star);
			} else if (picUrl.contains("dpfile.com/s/i/app/api/16_4star.png")) {
				iv_dazhongxingji.setImageResource(R.drawable.dazhong16_4star);
			} else if (picUrl.contains("dpfile.com/s/i/app/api/16_3star.png")) {
				iv_dazhongxingji.setImageResource(R.drawable.dazhong16_3star);
			} else {
				imageLoader.displayImage(picUrl, iv_dazhongxingji);
			}
			ll_dazhongclick.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

				}
			});
			return view;
		}
	}

	public String DateParse(String str1) {
		String[] space = str1.trim().split(" ");
		String[] arrMidLine = space[0].split("-");
		StringBuilder str = new StringBuilder();
		for (int i = 0; i < arrMidLine.length; i++) {
			if (i == 0)
				str.append(arrMidLine[i]).append("年");
			else if (i == 1) {
				str.append(arrMidLine[i]).append("月");
			} else if (i == 2) {
				str.append(arrMidLine[i]).append("日");
			}
		}
		for (int k = 1; k < space.length; k++) {
			str.append(" " + space[k]);
		}
		return str.toString();

	}
}